package com.mvc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mvc.model.Tribute;
import com.mvc.model.User;
import com.mvc.model.Venue;
import com.mvc.model.Worship;
import com.mvc.request.ReqPage;
import com.mvc.request.ReqVenueAddTribute;
import com.mvc.request.ReqVenueWorshipPage;
import com.mvc.request.ReturnData;
import com.mvc.response.RspPage;
import com.mvc.response.RspWorship;
import com.mvc.service.SacrificeService;

/***
 * 祭拜记录
 * 
 * @author Swer
 * 
 */
public class SacrificeServiceImpl implements SacrificeService {

	/***
	 * 删除祭拜记录[由删除场馆时调用]
	 * 
	 * @return
	 */
	public boolean deleteVenue(int venueId) {
		return false;
	}

	/**
	 * 场馆新增祭拜记录[由场馆添加祭品时调用]
	 * 
	 * @param message
	 * @return
	 */
	public ReturnData<Boolean> create(ReqVenueAddTribute req) {

		int venueId = req.getVenueId();
		int userId = req.getUserId();
		int tributeId = req.getGoods().getId();

		Worship sacrifice = new Worship();
		sacrifice.set(Worship.TableField.CREATETIME, new Date());
		sacrifice.set(Worship.TableField.TRIBUTEID, tributeId);
		sacrifice.set(Worship.TableField.USERID, userId);
		sacrifice.set(Worship.TableField.VENUEID, venueId);
		if (10000  == req.getUserId() )
		{
			// 游客-自动产生游客名称
		    String	  name ="游客" + (int)((Math.random()*9+1)*10000);
		    sacrifice.set(Worship.TableField.NAME, name);
		}
		
		if (sacrifice.save()) {
			Venue venue = Venue.dao.findById(venueId);
			if (venue != null) {
				// 更新怀念币数量
				int money = venue.getInt(Venue.TableField.MONEY)
						+ req.getGoods().getMoney();
				// 更新祭拜数量
				int worshipSize = venue.getInt(Venue.TableField.WORSHIPSIZE) + 1;
				int grade = venue.getInt(Venue.TableField.GRADE);

				// 　达到某个等级所需总经验=(等级+1)*等级*10
				if ((money + worshipSize) >= Math.abs((grade + 1) * grade * 10)) {
					grade += 1;
				}

				venue.set(Venue.TableField.WORSHIPSIZE, worshipSize);
				venue.set(Venue.TableField.MONEY, money);
				venue.set(Venue.TableField.GRADE, grade);
				venue.update();

			}
			return new ReturnData<Boolean>(true, 200, "祭拜成功");
		} else {
			return new ReturnData<Boolean>(false, 201, "祭拜失败");
		}

	}

	/***
	 * 获取最新的祭拜记录【分页】
	 * 
	 * @return
	 */
	public ReturnData<RspPage<RspWorship>> queryNewPage(ReqPage req) {
		Page<Worship> page = Worship.dao.queryNewPage(req.getNowPage(),
				req.getPageSize());
		RspPage<RspWorship> venuePage = new RspPage<>();
		venuePage.setNowPage(req.getNowPage());
		venuePage.setTotalPage(page.getTotalPage());
		venuePage.setList(getList(page.getList()));
		return new ReturnData<RspPage<RspWorship>>(venuePage, 200, "ok");
	}

	/***
	 * 获取场馆祭拜记录【分页】
	 * 
	 * @return
	 */
	public ReturnData<RspPage<RspWorship>> queryVenuePage(
			ReqVenueWorshipPage req) {
		Page<Worship> page = Worship.dao.queryVenuePage(req.getNowPage(),
				req.getPageSize(), req.getVenueId());
		RspPage<RspWorship> venuePage = new RspPage<>();
		venuePage.setNowPage(req.getNowPage());
		venuePage.setTotalPage(page.getTotalPage());
		venuePage.setList(getList(page.getList()));
		return new ReturnData<RspPage<RspWorship>>(venuePage, 200, "ok");
	}

	private List<RspWorship> getList(List<Worship> list) {
		List<RspWorship> data = new ArrayList<RspWorship>();
		for (Worship sacrifice : list) {
			RspWorship worship = new RspWorship();
			worship.setCreateTime(sacrifice
					.getDate(Worship.TableField.CREATETIME));
			worship.setId(sacrifice.getInt(Worship.TableField.ID));

			Venue venue = sacrifice.getVenue();
			if (venue == null) {
				continue;
			}

			worship.setVenueId(venue.getInt(Venue.TableField.ID));
			worship.setVenueName(venue.getStr(Venue.TableField.NAME));

			Tribute tribute = sacrifice.getTribute();
			if (tribute != null) {
				worship.setTributeName(tribute.getStr(Tribute.TableField.NAME));
			}

			User user = sacrifice.getUser();
			if (user != null) {
				worship.setUserHead(user.getStr(User.TableField.HEAD));
				int type = 	user.getInt(User.TableField.TYPE);
				String name = user.getStr(User.TableField.NAME);
				if (type ==3)
				{
					//游客-名称从留言里面获取
					String tempName =sacrifice.getStr(Worship.TableField.NAME);
					if (StrKit.notBlank(tempName))
					{
						name = tempName;
					}
				}
				worship.setUserName(name);
				worship.setUserId(user.getInt(User.TableField.ID));
			}

			data.add(worship);
		}
		return data;
	}
}
